package com.sangu.dianxiu.utils;

import android.os.Environment;

import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.config.Constants;

import java.io.File;
import java.io.IOException;

/**
 * 文件工具类
 * Created by sg2 on 2016/1/13.
 */
public class FileUtils {
    //获得缓存根目录
    public static String getRootCache()
    {
        File externalCacheDir= BaseApp.getInstance().getExternalCacheDir();
        if(!externalCacheDir.exists())
        {
            externalCacheDir.mkdirs();
        }
        return externalCacheDir.getAbsolutePath();

    }
    //获得图片缓存目录
    public static File getImageCache()
    {
        String imageCache=getRootCache()+File.separator+ Constants.APP_IMAGE_CACHE;
        File file=new File(imageCache);
        if(!file.exists())
        {
            file.mkdir();
        }
        return file;
    }
    //获得app文件下载路径
    public static String getAppDownloadPath(){
        String appPath=null;
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            appPath=Environment.getExternalStorageDirectory()+ File.separator+"diandianxiu"+File.separator+"download";
        }else{
            appPath=Environment.getDataDirectory()+File.separator+"com.sangu.dianxiu"+File.separator+"download";
        }
        if(!new File(appPath).exists())
        {
            new File(appPath).mkdirs();
        }
        return appPath;
    }
    //根据传递的url来获得app下载的文件对象
    public static File getAppDownloadPath(String url)
    {
        String voicePath= getAppDownloadPath()+File.separator+url.substring(url.lastIndexOf(File.separator));
        File voiceFile=new File(voicePath);
        if(!voiceFile.exists())
        {
            try {
                voiceFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return voiceFile;
    }
}
